package com.bgds.sys.module.controller;


import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.api.bean.mars.dto.DataTimeSerise;
import com.bgds.sys.api.service.DeTongService;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.pagevo.SeriesVo;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author lj
 */
@Slf4j
@RestController
@Api(tags = "除尘风机监测接口")
@RequestMapping("/dedustFan")
public class DedustFanController {
    @Autowired
    private IDeviceDynamicAttrService iDeviceDynamicAttrService;
    @Autowired
    private IAlarmLogService iAlarmLogService;
    @Autowired
    private InfluxService influxService;
    @Autowired
    private DeTongService deTongService;
    @RequestMapping(value = "/temperature", method = RequestMethod.GET)
    @ApiOperation(value = "获取除尘风机温度监测数据",notes = "获取助燃风机温度监测数据",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", required = true, value = "设备编号(code)"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间"),
    })

    public Result<Map<String,Object>> getDedustFanTemperature(String deviceCode, Long startTime, Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"temperature");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,deviceDynamicAttrs);
        map.put("data",chartData);
        map.put("deviceDynamicAttrs",deviceDynamicAttrs);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }

    @RequestMapping(value = "/trend", method = RequestMethod.GET)
    @ApiOperation(value = "除尘风机趋势分析数据",notes = "除尘风机趋势分析数据",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String",required = true, value = "设备编码"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间"),
    })


    public Result<Map<String,Object>> getDeviceChartData(String deviceCode, Long startTime,Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"trend");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,deviceDynamicAttrs);
        map.put("data",chartData);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }

    @RequestMapping(value = "/getLastVibData", method = RequestMethod.GET)
    @ApiOperation(value = "振动总貌分析",notes = "振动总貌分析",httpMethod = "GET",response = Result.class)
    public Result getLastVibData(String id){
       List<DataTimeSerise> dataTimeSerises= deTongService.getLastVibData(id);
       return new ResultUtil<>().setData(dataTimeSerises);
    }
    @RequestMapping(value = "/getLastTempData", method = RequestMethod.GET)
    @ApiOperation(value = "温度总貌分析",notes = "温度总貌分析",httpMethod = "GET",response = Result.class)
    public Result getLastTempData(String id){
        List<DataTimeSerise> dataTimeSerises= deTongService.getLastTempData(id);
        return new ResultUtil<>().setData(dataTimeSerises);
    }
}
